/*

    I2C interface
    Updated by Gareth Douglas 06/07/10
    gd78@le.ac.uk

*/

#include <io.h>

#include "i2c.h"

#include "../timing/timing.h"


int i2c_send()
{
  P3DIR = 0x0A;//Configure I2C pins to output, this will need to be changed to receive data
  P3SEL = 0x0A;//Sets pins to use peripheral module

  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTHOLD;

  BCSCTL2 |= SELS; //Set SMCLK to XT2 oscillator
  BCSCTL2 &= ~0x06; //Set clock divider to 1

  U0CTL |= SYNC + I2C; //Set USART 0 to I2C
  U0CTL |= MST; //Make I2C master

  U0CTL &= ~(I2CEN); //Clear I2C enable
  I2CTCTL |= 0x20; //Set I2C to use SMCLK
  I2CTCTL &= ~(I2CRM); //Set repeat mode to zero
  I2CTCTL &= ~(I2CWORD); //Set to byte mode
  I2CPSC = 0x3; //Set prescalar multiplier to 4x
  I2CSCLH = 0x8; //Set SCL high period to 8x prescalar period
  I2CSCLL = 0x8; //Set SCL low period to  8x prescalar period
  I2COA = 0x01; //Set own address to 1
  I2CSA = 0x41; //Set slave address
  U0CTL |= I2CEN; //Renable I2C

  waitms(100); //Wait for I2C to be ready

  I2CTCTL |= I2CTRX; //Set transmit mode
  I2CNDAT = 0x1; //Set to transmit one byte
  I2CDRB = 0x55; //Gives a data byte
  I2CTCTL |= I2CSTP; //Sets STOP condition bit
  I2CTCTL |= I2CSTT; //Initiates a start condition
}
